using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using au.Swindles.LoanDataService.ObjectLayer;
using System.Data.SqlClient;

namespace au.Swindles.LoanDataService.AdapterLayer
{
    public static class QuoteAdapter
    {
        private static string getConnectionString()
        {
            return ConfigurationManager.AppSettings["DBConnection"];
        }

        private static SqlParameter getSqlInputParameter(string pParameterName, object pParameterValue, SqlDbType pParameterType)
        {
            SqlParameter param = new SqlParameter();
            param.ParameterName = pParameterName;
            param.Direction = ParameterDirection.Input;
            param.SqlDbType = pParameterType;
            param.Value = pParameterValue;

            return param;
        }

        internal static int getQuoteByQuoteNumber(string pQuoteNumber)
        {
            
            SqlConnection connection = new SqlConnection(getConnectionString());
            try
            {
                string commandtext = "SELECT QuoteID FROM [dbo].[tblQUOTE] WHERE QuoteNumber = @pQuoteNumber";

                SqlCommand command = new SqlCommand(commandtext, connection);
                command.Parameters.Add(getSqlInputParameter("@pQuoteNumber", pQuoteNumber, SqlDbType.VarChar));
                connection.Open();
                int quoteID = Utilities.getInt32(command.ExecuteScalar());
                connection.Close();
                return quoteID;
            }
            catch (Exception)
            {
                connection.Close();
                return 0;
            }
           
        }

        internal static int getCustomerByCustomerNumber(string pCustomerNumber)
        {

            SqlConnection connection = new SqlConnection(getConnectionString());
            try
            {
                string commandtext = "SELECT CustomerID FROM [dbo].[tblCustomer] WHERE CustomerNumber = @pCustomerNumber";

                SqlCommand command = new SqlCommand(commandtext, connection);
                command.Parameters.Add(getSqlInputParameter("@pCustomerNumber", pCustomerNumber, SqlDbType.VarChar));
                connection.Open();
                int customerID = Utilities.getInt32(command.ExecuteScalar());
                connection.Close();
                return customerID;
            }
            catch (Exception)
            {
                connection.Close();
                return 0;
            }

        }
    }
}
